package com.aierjun.wedget.beisaier;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.Button;

import static android.R.attr.animation;
import static android.R.attr.button;
import static android.R.attr.width;
import static com.aierjun.wedget.R.attr.height;


/**
 * Created by Jam on 16-7-12
 * Description:
 */
public class Bezier2View extends View {
    private Point assistPoint; //控制点
    private Paint mPaint; //画笔
    private Path mPath; //路径
    private Point startPoint; //起点
    private Point endPoint;  //终点

    public Bezier2View(Context context) {
        super(context);
        init();
    }

    public Bezier2View(Context contex, AttributeSet attr) {
        super(contex, attr);
        init();
    }
    /**
     * 100         startPoint(300,100)               endPoint(900,100)
     *
     *
     *
     * 300                      assistPoint(600,300)
     *
     */
    private void init() {
        mPaint = new Paint();
        mPath = new Path();
        startPoint = new Point(300, 100);
        endPoint = new Point(900, 100);
        assistPoint = new Point(600, 300);
        // 抗锯齿
        mPaint.setAntiAlias(true);
        // 防抖动
        mPaint.setDither(true);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        mPaint.setColor(Color.RED); //画笔颜色
//        mPaint.setStrokeWidth(10); //画笔宽度
//        mPaint.setStyle(Paint.Style.STROKE);
//
//        mPath.reset();
//        //起点
//        mPath.moveTo(startPoint.x, startPoint.y);
//        //mPath
//        mPath.quadTo(assistPoint.x, assistPoint.y, endPoint.x, endPoint.y);
//        //画path
//        canvas.drawPath(mPath, mPaint);
//        //画控制点
//        canvas.drawPoint(assistPoint.x, assistPoint.y, mPaint);

        //画线
//        canvas.drawLine(startPoint.x, startPoint.y, assistPoint.x, assistPoint.y, mPaint);
//        canvas.drawLine(endPoint.x, endPoint.y, assistPoint.x, assistPoint.y, mPaint);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                assistPoint.x = (int) event.getX();
                assistPoint.y = (int) event.getY();
                invalidate();
                break;
        }
        return true;
    }

    public static void move(final Button button){
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setEvaluator(new BezierEvaluator());
        valueAnimator.setInterpolator(new Interpolator() {
            @Override
            public float getInterpolation(float v) {
                return 3;
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF)animation.getAnimatedValue();
                button.setX(pointF.x);
                button.setY(pointF.y);
            }
        });
    }

}

class BezierEvaluator implements TypeEvaluator<PointF> {

    @Override
    public PointF evaluate(float fraction, PointF startValue,
                           PointF endValue) {
        final float t = fraction;
        float oneMinusT = 1.0f - t;
        PointF point = new PointF();

        PointF point0 = (PointF)startValue;

        PointF point1 = new PointF();
        point1.set(width, 0);

        PointF point2 = new PointF();
        point2.set(0, height);

        PointF point3 = (PointF)endValue;

        point.x = oneMinusT * oneMinusT * oneMinusT * (point0.x)
                + 3 * oneMinusT * oneMinusT * t * (point1.x)
                + 3 * oneMinusT * t * t * (point2.x)
                + t * t * t * (point3.x);

        point.y = oneMinusT * oneMinusT * oneMinusT * (point0.y)
                + 3 * oneMinusT * oneMinusT * t * (point1.y)
                + 3 * oneMinusT * t * t * (point2.y)
                + t * t * t * (point3.y);
        return point;
    }
}